﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DataContext;

namespace WorkFlowBLL
{
    public class CtrBase
    {
            
        public int UnitInsert(string unitName, int? multiphy,int? baseUnitID)
        {
            return DAL.BaseInstance.CreateInstance().uspUnitInsert(unitName, multiphy, baseUnitID);
        }

        public int UnitUpdate(int unitID, string unitName, int? multiphy, int? baseUnitID)
        {
            return DAL.BaseInstance.CreateInstance().uspUnitUpdateByUnitID(unitID, unitName,multiphy,baseUnitID);
        }
        
        public int UnitDelete(int unitID)
        {
            return DAL.BaseInstance.CreateInstance().uspUnitDeleteByUnitID(unitID);
        }
        
        public uspUnitGetInfoByUnitIDResult UnitGetInfo(int unitID)
        {
            return DAL.BaseInstance.CreateInstance().uspUnitGetInfoByUnitID(unitID).FirstOrDefault();
        }
        
        public bool CheckExistUnitProduct(int unitID)
        {
            int? ret = 0;
            DAL.BaseInstance.CreateInstance().uspUnitCheckExistProduct(unitID, ref ret);
            if (ret != null) return (int)ret == 1;
            return false;
        }

        public List<uspStockGetAllResult> GetListStock()
        {
            return DAL.BaseInstance.CreateInstance().uspStockGetAll().ToList();
        }

        public List<uspDistributeGetAllResult> GetListDistribution()
        {
            return DAL.BaseInstance.CreateInstance().uspDistributeGetAll().ToList();
        }

        public int StockInsert(int totalMoney, int distributeID, string description,DateTime createDate, int userID, int status)
        {
            return DAL.BaseInstance.CreateInstance().uspStockInsert(totalMoney, distributeID, description, createDate, userID, status);
        }

        public int StockUpdate(int stockId, int totalMoney, int distributeID, string description, DateTime createDate, int userID, int status)
        {
            return DAL.BaseInstance.CreateInstance().uspStockUpdateByStockID(stockId, totalMoney, distributeID, description, createDate, userID, status);
        }
        public int StockDetailInsert(int stockID,int ProductID,string productName,double quantity,int price,DateTime createDate)
        {
            return DAL.BaseInstance.CreateInstance().uspStockDetailInsert(stockID, ProductID, productName, quantity, price, createDate);
        }
        public int StockDetailUpdate(int stockDetailID, int ProductID, double quantity, int price, DateTime createDate)
        {
            return DAL.BaseInstance.CreateInstance().uspStockDetailUpdateByStockDetailID(stockDetailID,  ProductID, quantity, price, createDate);
        }

        public int StockDelete(int stockID)
        {
            return DAL.BaseInstance.CreateInstance().uspStockDeleteByStockID(stockID);
        }

        public int StockDetailDelete(int stockID, int productID)
        {
            return DAL.BaseInstance.CreateInstance().uspStockDetailDelete(stockID, productID);
        }

        public int StockDetailDeleteMulti(int stockID, string productIDs)
        {
            return DAL.BaseInstance.CreateInstance().uspStockDetailDeleteMulti(stockID, productIDs);
        }

        public List<uspStockGetAllResult> StockGetAll()
        {
            return DAL.BaseInstance.CreateInstance().uspStockGetAll().ToList();
        }

        public List<uspStockDetailGetAllByStockIDResult> StockDetailGetList(int stockID, ref int? totalMoney)
        {
            return DAL.BaseInstance.CreateInstance().uspStockDetailGetAllByStockID(stockID, ref totalMoney).ToList();
        }

        public List<uspStockGetAllSearchResult> GetAllStockSearch(DateTime fromDate, DateTime toDate, int adminID,ref int? totalMoney)
        {
            return DAL.BaseInstance.CreateInstance().uspStockGetAllSearch(fromDate, toDate, adminID, ref totalMoney).ToList();
        }
        
        public int CheckExistStockDetail(int stockID, int productID)
        {
            int? ret = 0;
            DAL.BaseInstance.CreateInstance().uspStockDetailExists(stockID, productID, ref ret);
            return (int)ret;
        }

        public uspStockGetInfoByStockIDResult GetStockInfoByStockID(int stockID)
        {
            return DAL.BaseInstance.CreateInstance().uspStockGetInfoByStockID(stockID).FirstOrDefault();
        }

        public List<uspStockReportAllSearchResult> GetAllReportStockSearch(string productName, int categoryID)
        {
            return DAL.BaseInstance.CreateInstance().uspStockReportAllSearch(productName, categoryID).ToList();
        }

        public int DistributeInsert(string distributeName, string distributePhone, string distributeAddress, int status)
        {
            return DAL.BaseInstance.CreateInstance().uspDistributeInsert(distributeName, distributePhone, distributeAddress, status);
        }

        public int DistributeUpdate(int distributeID, string distributeName, string distributePhone, string distributeAddress, int status)
        {
            return DAL.BaseInstance.CreateInstance().uspDistributeUpdateByDistributeID(distributeID, distributeName, distributePhone, distributeAddress, status);
        }

        public int DistributeDelete(int distributeID)
        {
            return DAL.BaseInstance.CreateInstance().uspDistributeDeleteByDistributeID(distributeID);
        }

        public int StockDetailExist(int stockId, int productId,  ref int? ret)
        {
            return DAL.BaseInstance.CreateInstance().uspStockDetailExistStock(stockId, productId, ref ret);
        }

        public List<uspDistributeGetAllResult> DistributeGetAll()
        {
            return DAL.BaseInstance.CreateInstance().uspDistributeGetAll().ToList();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="baseUnitID">-1: Tất cả, 0: Lấy các Unit cơ bản, >0: Lấy các Unit là tỉ lệ nhân của Base Unit</param>
        /// <returns></returns>
        public List<uspUnitGetByBaseUnitIDResult> GetUnitByBaseUnitID(int baseUnitID)
        {
            return DAL.BaseInstance.CreateInstance().uspUnitGetByBaseUnitID(baseUnitID).ToList();
        }

        public uspDistributeGetInfoByDistributeIDResult DistributeGetInfo(int distributeID)
        {
            return DAL.BaseInstance.CreateInstance().uspDistributeGetInfoByDistributeID(distributeID).FirstOrDefault();
        }

        public uspStockDetailGetInfoByStockDetailIDResult GetStockDetail(int stockDetailID)
        {
            return DAL.BaseInstance.CreateInstance().uspStockDetailGetInfoByStockDetailID(stockDetailID).FirstOrDefault();
        }
    }
}
